<template>
  <div class="login-container">
      <div class="logo">
        <!-- <img src="./images/logo.png" alt="Vue"> -->
        <slot></slot>
        <h1>商城后台管理</h1>
      </div>
      <el-form label-position="left" :rules="loginRules" ref="ruleForm" :model="formLogin">
          <el-form-item prop="name">
            <el-input v-model="formLogin.name" placeholder="请输入用户名" @keyup.enter.native="submitForm('ruleForm')"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="formLogin.password" type="password" placeholder="请输入密码" @keyup.enter.native="submitForm('ruleForm')"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="submitForm('ruleForm')">登录</el-button>
          </el-form-item>
      </el-form>
  </div>
</template>

<script>
import {login} from '@/services/login'
import {RequestError} from '@/services/errors'
export default {
  data: () => ({
    formLogin: {
      name: '',
      password: ''
    },
    loginRules: {
      name: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 4,max: 50, message: '长度在 4到 50 个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '长度在 6个字符以上', trigger: 'blur' }
      ]
    }
  }),
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          try {
            await login(this.formLogin)
            this.$message({
              message: '欢迎使用壹商城管理平台',
              type: 'success'
            })
            if (this.$route.query.redirect) {
              this.$router.push(this.$route.query.redirect)
            } else {
              this.$router.push({name: 'app'})
            }
          } catch (e) {
            if (e instanceof RequestError) {
              this.$alert(e.message, '消息提示', {
                confirmButtonText: '确定'
              })
            } else {
              throw e
            }
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm () {}
  }
}
</script>

<style lang="less" scoped>
  .login-container {
    width: 320px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    .logo {
      width: 200px;
      margin: 0 auto 20px;
      color: #666;
      img {
        max-width: 40%;
      }
    }
    .el-form-item__content {
      .el-button {
        width: 100%;
      }
    }
  }
</style>
